package cn.relx.service.controller;

import cn.relx.basic.util.LoginContext;
import cn.relx.org.domain.Employee;
import cn.relx.org.mapper.EmployeeMapper;
import cn.relx.service.domain.Product;
import cn.relx.service.service.IOrderProductService;
import cn.relx.service.domain.OrderProduct;
import cn.relx.service.query.OrderProductQuery;
import cn.relx.basic.util.AjaxResult;
import cn.relx.basic.util.PageList;
import cn.relx.user.domain.Logininfo;
import cn.relx.user.domain.User;
import cn.relx.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    private IOrderProductService orderProductService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> list(){

        return orderProductService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderProduct> json(@RequestBody OrderProductQuery query)
    {
        return orderProductService.queryByPage(query);
    }

    @PostMapping("/buy")
    public AjaxResult buy(@RequestBody Product product, HttpServletRequest request)
    {
        Logininfo loginUser = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.findByLogininfoId(loginUser.getId());
        User user = userMapper.findByLogininfoId(loginUser.getId());
        orderProductService.addOrder(product,employee.getShopId(),user.getId());
        return AjaxResult.me().success();
    }

    @PostMapping("/tohandle")
    public PageList<OrderProduct> tohandle(@RequestBody OrderProductQuery query)
    {
        return orderProductService.findOrderProduct(query);
    }

    /**
     * 消除订单
     * @param id
     * @return
     */
    @PostMapping("/eliminate/{id}")
    public AjaxResult eliminate(@PathVariable("id") Long id)
    {
        orderProductService.updateEliminate(id);
        return AjaxResult.me().success();
    }

    @PostMapping("/ordersReceived/{id}")
    public AjaxResult ordersReceived(@PathVariable("id") Long id)
    {
        orderProductService.ordersReceived(id);
        return AjaxResult.me().success();
    }
}
